package com.learn.use.myreconsituteproject;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;
import com.learn.use.myreconsituteproject.login.widget.LoginActivity;
import com.learn.use.myreconsituteproject.util.SharedPreferencesUtil;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/9/11.
 * Time: 2018/9/11  14:47
 */
public class SplashActivity extends Activity {

    private String[] perms;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initData();

        perms = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.WAKE_LOCK,
                Manifest.permission.READ_PHONE_STATE};

        if (XXPermissions.isHasPermission(SplashActivity.this, perms)) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    finish();
                }
            }, 1500);
        } else {
            initView();
        }


    }

    private void initData() {
        String ase="--fsfwe8wwnhjgth348hgw89ty4ghfgvuehgeroigjer9ow845tmdfmapRUJHTW547TNK" +
                "AQLWQ48TU4NGVE294N5482JNFMNfnjej83hq834hjegnjeoghjtoewjhggrgerht347ty3ce8" +
                "7tyyvnnghdhslkgheoigngsdfjlwhfgerhsljfjagjnhglldgjsdkkdjgdhfnfneudnfgjsnai" +
                "eujfnbxdiwanjmfvndiuhandALWNJFUFNBNDAHBFDbnfedu345t3495t34rnb2wbnsf1q1r42eg" +
                "w0-9r23rtu290wjhgq90t23whgwngwqoeutg3489ghsogvnjqweotgy349ewgvsiuefythg2493" +
                "ghywoutf29hq0or8u9yhgefgjmwkwqvumzaflkiegjasmkscfuse9jvgfm8re0wvugearkcvutywm" +
                "e98tu9wccvgu9hjewcgk8uyerekvmychgfk8jgh7nccxdf6tbhcfnwhmxnecj7drf6ew7exmn6tre" +
                "7mkcjrteghe7xmkgrmkcdygrhfumcertgc7omfotygghirdfojhrhgdlohgijrtioghjdfklgbnmdf" +
                "lkhgbnjrtoihgjndflhgjndfghgblfgdjnhsldegjeogjsedogfhwaesogfthwesaofhw3euisfhg7" +
                "834hgfaw09q87thj0qw8tyu0aqw9utgmaqv8b9yfh427n8awvfaqg043rt12y499t" +
                "7cnyrq12wb7grtfnc2h9hwv7etyfgh2w9vtyegwamv0yt9ekbmjyuoghvesmnf87cmy8ven8g--";

        SharedPreferencesUtil.saveString(this,"ase_simple",ase);

    }

    private void initView() {
        XXPermissions.with(this)
                .permission(perms)
                .request(new OnPermission() {

                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        if (!isAll) {
                            Toast.makeText(SplashActivity.this, "获取权限成功，部分权限未正常授予", Toast.LENGTH_SHORT).show();
                            XXPermissions.gotoPermissionSettings(SplashActivity.this);
                        } else {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                                    finish();
                                }
                            }, 3000);
                        }

                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {

                        if (quick) {
                            Toast.makeText(SplashActivity.this, "被永久拒绝授权，请手动授予权限", Toast.LENGTH_SHORT).show();
                            //如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.gotoPermissionSettings(SplashActivity.this);
                        } else {
                            Toast.makeText(SplashActivity.this, "获取权限失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


}
